﻿namespace UnityEditor
{
    using System;
    using System.Diagnostics;
    using System.IO;
    using UnityEngine;

    internal class ExternalTool
    {
        internal static StreamReader StartProcess(string processFileName, string inputArguments, string outputFile, bool waitForExit, string workingDir)
        {
            Process process = new Process {
                StartInfo = { FileName = processFileName, Arguments = inputArguments, UseShellExecute = false, CreateNoWindow = true, RedirectStandardError = true, RedirectStandardOutput = true }
            };
            if (workingDir.Trim() != string.Empty)
            {
                process.StartInfo.WorkingDirectory = workingDir;
            }
            process.Start();
            if (waitForExit)
            {
                process.WaitForExit();
                if ((process.ExitCode != 0) || ((outputFile.Trim() != string.Empty) && !File.Exists(outputFile)))
                {
                    UnityEngine.Debug.Log(processFileName + " " + inputArguments);
                    string[] textArray1 = new string[] { processFileName, " -> ", outputFile, " : ", process.StandardError.ReadToEnd() };
                    throw new System.Exception(string.Concat(textArray1));
                }
            }
            return process.StandardOutput;
        }
    }
}

